package com.zeny.coding.dto;


import com.zeny.coding.util.DtoUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import javax.xml.bind.ValidationException;
import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class SectionDto {
    private String id;

    private String title;

    private String courseId;

    private String chapterId;

    private String video;

    private Integer time;

    private String charge;

    private Integer sort;

    private Date createdAt;

    private Date updatedAt;

    private String vod;


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", title=").append(title);
        sb.append(", courseId=").append(courseId);
        sb.append(", chapterId=").append(chapterId);
        sb.append(", video=").append(video);
        sb.append(", time=").append(time);
        sb.append(", charge=").append(charge);
        sb.append(", sort=").append(sort);
        sb.append(", createdAt=").append(createdAt);
        sb.append(", updatedAt=").append(updatedAt);
        sb.append(", vod=").append(vod);
        sb.append("]");
        return sb.toString();
    }

    public void isValidForSave() throws ValidationException {
        valid();
        if (!StringUtils.isEmpty(this.id)) {
            this.id = null;
        }
    }

    public void isValidForUpdate() throws ValidationException {
        DtoUtil.valid(this.id, true, 1, 19);
        valid();

    }

    private void valid() throws ValidationException {
        //检查标题
        DtoUtil.valid(this.title, true, 1, 50);
        //检查vod
        DtoUtil.valid(this.vod, false, 1, 32);
        //检查大章
        DtoUtil.valid(this.chapterId, true, 1, 19);
        //检查课程
        DtoUtil.valid(this.courseId, true, 1, 19);
        //检查排序
        DtoUtil.require(this.sort);
        //检查收费
        DtoUtil.valid(this.charge, true, 1, 1);
    }
}